package com.msk.common.core.smsclient.ali;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 阿里云短信接口
 * @Author zlf
 * @Date 2023/5/9 17:12
 */
public class AliSendSms {

    private static final Logger log = LoggerFactory.getLogger(AliSendSms.class);
    /**
     * 账号
     * AccessKey ID
     */
    private static String accessKeyId = "LTAI4FzCXBVfE5rmLwNZvdod";

    /**
     * 账号
     * AccessKey Secret
     */
    private static String accessSecret = "jYA08wmkGq62U7buq9f3drqgn8nMqR";

    /**
     * 请求地址
     */
    private static String HTTP_URL = "dysmsapi.aliyuncs.com";

    /**
     * 发送验证码,发送单个参数
     * @param phone 电话
     * @param code 验证码
     * @param templateCode 模板id
     * @return
     */
    public static Map<String,String> sendMessage(String phone,String code,String templateCode){
        Map<String, String> common = AliSendSms.common(phone, templateCode, null, null, code);
        return common;
    }

    /**
     * 发送通知,发送多个参数
     * @param phone 手机号码
     * @param middleman 医生的经纪人
     * @param platform 医生平台
     * @param templateCode 模板id
     * @return
     */
    public static Map<String,String> sendMessage(String phone,String templateCode,String middleman,String platform){
        Map<String, String> common = AliSendSms.common(phone, templateCode, middleman, platform, null);
        return common;
    }

    public static Map<String,String> common(String phone,String templateCode,String middleman,String platform,String code){

        Map<String,String> map = new HashMap<>();

        DefaultProfile defaultProfile = DefaultProfile.getProfile("cn-beijing", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(defaultProfile);

        //创建API请求并设置参数
        CommonRequest request = getCommonRequest(phone, templateCode, middleman, platform, code);

        //起请求并处理应答或异常
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
            JSONObject object = JSONObject.parseObject(response.getData());
            String code1 = object.getString("Code");
            String message = object.getString("Message");
            if (code1.equals("OK")){
                map.put("code", code1);
                map.put("message", message);
            }else{
                map.put("code", code1);
                map.put("message", message);
            }
        } catch (ServerException e) {
            log.error("phone:"+phone+",templateCode:"+templateCode+",middleman:"+middleman+",platform:"+platform+",code:"+code+"_"+e.getMessage(),e);
        } catch (ClientException e) {
            log.error("phone:"+phone+",templateCode:"+templateCode+",middleman:"+middleman+",platform:"+platform+",code:"+code+"_"+e.getMessage(),e);
        }
        return map;
    }

    /**
     * 设置请求参数
     * @param phone
     * @param templateCode
     * @param middleman
     * @param platform
     * @param code
     * @return
     */
    private static CommonRequest getCommonRequest(String phone, String templateCode, String middleman, String platform, String code) {
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        //请求地址
        request.setDomain(HTTP_URL);
        //请求版本
        request.setVersion("2017-05-25");
        //发送接口
        request.setAction("SendSms");
        //短信地区
        request.putQueryParameter("RegionId", "cn-beijing");
        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //短信签名名称。请在控制台签名管理页面签名名称一列查看。必须是已添加、并通过审核的短信签名。
        request.putQueryParameter("SignName", "推诚货运");
        //短信模板ID。请在控制台模板管理页面模板CODE一列查看。必须是已添加、并通过审核的短信签名；且发送国际/港澳台消息时，请使用国际/港澳台短信模版。
        request.putQueryParameter("TemplateCode", templateCode);
        if(code !=null){
            request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        }else if(middleman !=null && platform !=null){
            request.putQueryParameter("TemplateParam", "{\"middleman\":\""+middleman+"\", \"platform\":\""+platform+"\"}");
        }else{
            // todo 暂无业务
        }
        return request;
    }

    public static void main(String[] args) {

        /**
         * phone 要发送给那位专家的手机号码
         * middleman 被谁发送
         * platform 发送到什么地方，要干什么事情
         * templateCode 不需要改变，直接用
         */
//        Map<String, String> map = AliSendSms.sendMessage("18740492915", "SMS_180059361", "良方我吴哥", "米墨");
//        String isOk = map.get("code");
//        //发送成功之后,处理自己的业务
//        if (isOk.equals("OK")){
//
//        }
    }
}
